using System.Collections.Generic;
using UnityEngine;

namespace BCW.Friends
{
	public class FriendsUIChat : SelectableButtonController
	{
		public enum FriendsUIChatTypeUsing
		{
			lobby = 0,
			firstLevel = 1
		}

		public FriendsUIChatTypeUsing typeUsing;

		public FriendsUIPlayerChat playerChatButton;

		public FriendsUIMessageScroll messageScroll;

		public GameObject messageBG;

		public Transform content;

		public GameObject noFriendsOnlineLable;

		private int _lastIndex;

		public static FriendsUIChat GetInstance()
		{
			if ((bool)FriendsUIController.instance)
			{
				return FriendsUIController.instance.TabChat;
			}
			if ((bool)ChatWindow.instance)
			{
				return ChatWindow.instance.TabChat;
			}
			return null;
		}

		private void OnEnable()
		{
			FriendsController.OnUpdatePlayerDB += AddNewFriends;
			FriendsController.OnUpdateTimestamps += ResortFriends;
			if ((bool)messageScroll)
			{
				messageScroll.gameObject.SetActive(false);
			}
			playerChatButton.gameObject.SetActive(false);
			Init();
		}

		private void OnDisable()
		{
			FriendsController.OnUpdatePlayerDB -= AddNewFriends;
			FriendsController.OnUpdateTimestamps -= ResortFriends;
		}

		public void AddNewFriends()
		{
			List<PlayerInfo> fiendsList = FriendsController.instance.GetFiendsList();
			foreach (PlayerInfo item in fiendsList)
			{
				bool flag = false;
				foreach (SelectableButton allButton in allButtons)
				{
					if ((allButton as FriendsUIPlayerChat).playerInfo.playerId.Equals(item.playerId))
					{
						flag = true;
						break;
					}
				}
				if (!flag)
				{
					FriendsShowInListCheckerInterface checker = FriendsShowInListFactory.getChecker(typeUsing);
					if (checker.check(item))
					{
						InstantiateButton(item);
					}
				}
			}
		}

		public void Init()
		{
			ClearAllButtons();
			_lastIndex = 0;
			List<string> list = new List<string>();
			foreach (PlayerInfo fiends in FriendsController.instance.GetFiendsList())
			{
				FriendsShowInListCheckerInterface checker = FriendsShowInListFactory.getChecker(typeUsing);
				if (checker.check(fiends))
				{
					InstantiateButton(fiends);
					list.Add(fiends.playerId);
				}
			}
			if ((bool)FriendsController.instance && FriendsController.instance.IdsFrinedsInviteToBattle.Count > 0)
			{
				foreach (string item in FriendsController.instance.IdsFrinedsInviteToBattle)
				{
					if (!list.Contains(item))
					{
						FriendsController.instance.IdsFrinedsInviteToBattle.Remove(item);
					}
				}
			}
			if (allButtons.Count > 0)
			{
				allButtons[0].Select(true);
			}
			if ((bool)noFriendsOnlineLable)
			{
				noFriendsOnlineLable.SetActive(allButtons.Count == 0);
			}
			if ((bool)FriendsUIMessageManager.instance)
			{
				FriendsUIMessageManager.instance.OnChatsTabOpen(allButtons.Count);
			}
			SetEnabledChat(typeUsing == FriendsUIChatTypeUsing.firstLevel || allButtons.Count != 0);
		}

		private void InstantiateButton(PlayerInfo player)
		{
			FriendsUIPlayerChat friendsUIPlayerChat = Object.Instantiate(playerChatButton);
			friendsUIPlayerChat.transform.SetParent(content);
			friendsUIPlayerChat.transform.localScale = Vector3.one;
			friendsUIPlayerChat.transform.localPosition = new Vector3(0f, -_lastIndex * 78, 0f);
			friendsUIPlayerChat.Init(player);
			AddNewButton(friendsUIPlayerChat);
			friendsUIPlayerChat.gameObject.SetActive(true);
			_lastIndex++;
		}

		private void ResortFriends()
		{
			List<PlayerInfo> fiendsList = FriendsController.instance.GetFiendsList();
			int num = 0;
			PlayerInfo tmpPlayerInfo;
			foreach (PlayerInfo fiends in FriendsController.instance.GetFiendsList())
			{
				tmpPlayerInfo = fiends;
				SelectableButton selectableButton = allButtons.Find((SelectableButton x) => ((FriendsUIPlayerChat)x).playerInfo.playerId.Equals(tmpPlayerInfo.playerId));
				if (selectableButton != null)
				{
					selectableButton.transform.localPosition = Vector3.down * num;
					num += 78;
				}
			}
		}

		private void PullUpFriend(string id)
		{
			int num = allButtons.FindIndex((SelectableButton x) => ((FriendsUIPlayerChat)x).playerInfo.playerId.Equals(id));
			SelectableButton value = allButtons[num];
			for (int num2 = num; num2 >= 0; num2--)
			{
				allButtons[num + 1] = allButtons[num];
				allButtons[num].transform.position += Vector3.down * 78f;
			}
			allButtons[0] = value;
			allButtons[0].transform.position = Vector3.down * 78f;
		}

		public bool OpenChatWith(string id)
		{
			FriendsUIController.instance.SeletctButtonByType(FriendsUIController.FriendUITab.Chat);
			return SelectButton(allButtons.Find((SelectableButton x) => x.id == id));
		}

		public void SetEnabledChat(bool isEnabled)
		{
			messageScroll.gameObject.SetActive(isEnabled);
			if (messageBG != null)
			{
				messageBG.SetActive(isEnabled);
			}
			content.gameObject.SetActive(isEnabled);
		}
	}
}
